package com.garrett.demo.sys.service;

import com.garrett.demo.sys.dao.UserDao;
import com.garrett.demo.sys.dao.UserDaoTextImp;
import com.garrett.demo.sys.dao.UserDataBaseDemo;
import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImp implements AccountService{

    private UserDao userDao;
    /**
     * 验证信息
     *
     * @param account  账户名
     * @param password 密码
     * @return 用户信息
     */
    @Override
    public SimsStudent checkLogin(String account, String password) {

        userDao = new UserDataBaseDemo();
        Account ac = userDao.getUserByAccount(account);

        // 这里注意我们要用到空值判断，后期我们会优化掉这一部分
        if (null != ac && ac.getPassword().equals(password)) {
            userDao = new UserDaoTextImp();
            return userDao.getUserById(ac.getId());
        }

        return null;
    }

    /**
     * 注册账户
     *
     * @param obj 账户信息
     */
    @Override
    public void createAccount(Object... obj) {
        System.out.println("UserServiceImp: insert user");
        if(obj.length > 0){
            Object o = obj[0];
            SimsStudent student = o instanceof SimsStudent ? (SimsStudent) obj[0] : new SimsStudent();
            userDao = new UserDaoTextImp();
            userDao.createUser(student);

        }
    }
}
